/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.supinfo.suptracking.servlet;

import com.supinfo.suptracking.entity.Car;
import com.supinfo.suptracking.entity.Gpspositionhistory;
import com.supinfo.suptracking.service.CarService;
import com.supinfo.suptracking.service.GpsPositionHistoryService;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Ludovic
 */
@WebServlet(name = "ListGpsPositionHistoryServlet", urlPatterns = {"/auth/listGpsPositionHistoryServlet"})
public class ListGpsPositionHistoryServlet extends HttpServlet {
        @EJB
        private GpsPositionHistoryService gpsServ;
        
        @EJB
        private CarService carServ;

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        List<Gpspositionhistory> listPos = new ArrayList() ;
        List<Car> listCars = (List<Car>) request.getSession().getAttribute("listCars");
        System.out.println("taille de la liste " + listCars.size());
        listCars.stream().forEach((car) -> {
            List<Gpspositionhistory> listPostmp = gpsServ.processGetAllPositionHistoryByCar(car);
            System.out.println("taille de la liste " + listPostmp.size() +"car id= " + car.getId()); 
            if(listPostmp.size() < 10)
                {
                    listPos.addAll(listPostmp);
                }else{
                listPos.addAll(listPostmp.subList((listPostmp.size()-10), listPostmp.size()));           
            }
        });
        System.out.println("taille de la liste " + listPos.size());      
        request.setAttribute("listPos", listPos);
        request.getRequestDispatcher("/jsp/auth/ListGpsPos.jsp").forward(request, response);

    }

  
}
